package service

import (
	"app/internal/channel"
	"app/util"
	"errors"
)

func (s *Service) WaitMsg() error {

	if s.dao == nil {
		util.Error(daoErrorString)
		return errors.New(daoErrorString)
	}

	ms := util.NewMessageloop(4)

	go func() {
		defer ms.Signal()
		s.WaitChannelMsg(channel.RegisterChannel, s.registerChannel)
	}()
	go func() {
		defer ms.Signal()
		s.WaitChannelMsg(channel.UserChannel, s.userChannel)
	}()
	go func() {
		defer ms.Signal()
		s.WaitChannelMsg(channel.HeartChannel, s.heartChannel)
	}()
	go func() {
		defer ms.Signal()
		s.WaitChannelMsg(channel.LicenseChannel, s.licenseChannel)
	}()

	go func() {
		defer ms.Signal()
		s.WaitChannelMsg(channel.SyncLicenseChannel, s.licenseChannel)
	}()

	ms.Wait()

	return nil
}
